package com.zhang.handler;

import com.zhang.ChannelManage;
import com.zhang.common.Constant;
import com.zhang.common.message.ProxyMessage;
import com.zhang.common.message.ProxyMessageUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 98265
 */
public class TCPHandler extends ChannelInboundHandlerAdapter {

    /**
     * 日志对象
     */
    static private Logger log = LoggerFactory.getLogger(TCPHandler.class);
    /**
     * 用户连接真实服务器
     */
    private Bootstrap realBootstrap;

    /**
     * 真实服务器的ip
     */
    private String realServerIp;
    /**
     * 真实服务器的端口
     */
    private int realServerPort;

    public TCPHandler(Bootstrap realBootstrap, String realServerIp, int realServerPort) {
        this.realBootstrap = realBootstrap;
        this.realServerIp = realServerIp;
        this.realServerPort = realServerPort;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ProxyMessage proxyMessage = (ProxyMessage) msg;
        Byte msgType = proxyMessage.getMsgType();
        Long httpId = proxyMessage.getHttpId();

        switch (msgType) {
            case Constant
                    .MessageType.TYPE_CONNECT_REAL_SERVER_REQUEST:
                //连接真实服务器
                realBootstrap.connect(realServerIp,realServerPort).addListener((ChannelFutureListener) (future)->{
                    ProxyMessage response;
                    if(future.isSuccess()){
                        log.info("连接真实服务器成功");
                        Channel channel = future.channel();
                        ChannelManage.setRealServerChannel(channel);
                        //成功
                        response = ProxyMessageUtil.buildConnectRealServerRespSuccess(httpId);
                        ctx.writeAndFlush(response);
                        return;
                    }else{
                        //失败
                        response = ProxyMessageUtil.buildConnectRealServerRespFail(httpId);
                       ctx.writeAndFlush(response);
                    }
                });
                break;
            case Constant.MessageType.TYPE_TRANSFER_REQUEST:
                //转发请求给真实服务器
                Channel realServerChannel = ChannelManage.getRealServerChannel();
                if(null == realServerChannel){

                    ctx.writeAndFlush(
                            ProxyMessageUtil.buildTransferRespFail(httpId)
                    );
                    log.error("转发真实服务器失败");
                    return;
                }
                //http请求数据
                byte[] bytes = proxyMessage.getData();
                realServerChannel.attr(Constant.HTTP_ID).set(httpId);
                //往下 走http处理
                ctx.fireChannelRead(Unpooled.wrappedBuffer(bytes));
                break;
            default:
                break;
        }

    }
}
